package com.ruoyi.boat.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * datacenter
 *
 * 4/2/2022 - 11:28 AM
 */
public class DateUtil {

    public static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_PATTERN = "yyyy-MM-dd";
    public static String TIME_PATTERN = "HH:mm:ss";

    public static Date stringToDate(String dateStr, Date defaultValue) {
        if (dateStr == null || dateStr.isEmpty()) return defaultValue;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return defaultValue;
        }
    }

    public static String date2String(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static Date string2Date(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String localDateTime2String(LocalDateTime date, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return date.format(formatter);
    }

    public static Date dateToISODate(Date date) {
        Date parse = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            parse = format.parse(format.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static Date timestampToDate(long timestamp){
        return new Date(timestamp * 1000);
    }
}
